package service

import (
	"singo/model"
	"singo/serializer"
)

// ManageProjectService 项目管理列表服务
type ManageProjectService struct {
	Limit int `form:"pageSize" json:"pageSize"`
	Start int `form:"pageNo" json:"pageNo"`
}

// ManageProjectList 项目管理列表
func (service *ManageProjectService) ManageProjectList() serializer.Response {
	manageProjects := []model.ManageProject{}
	projects := []model.Project{}
	total := 0
	task_total := 0
	task_done := 0
	demand_total := 0
	demand_done := 0
	defect_total := 0
	defect_done := 0
	overdue_total := 0

	if service.Limit == 0 {
		service.Limit = 10
	}

	if err := model.DB.Model(&projects).Count(&total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Find(&projects).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	for _, item := range projects {
		// 项目管理任务数统计
		if err := model.DB.Model(&model.Task{}).Where("project_id = ?", item.UID).Count(&task_total).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}
		if err := model.DB.Model(&model.Task{}).Where("status = ? AND project_id = ?", 3, item.UID).Count(&task_done).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}

		// 项目管理需求数统计
		if err := model.DB.Model(&model.Demand{}).Where("project_id = ?", item.UID).Count(&demand_total).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}
		if err := model.DB.Model(&model.Demand{}).Where("status = ? AND project_id = ?", 3, item.UID).Count(&demand_done).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}

		// 项目管理缺陷数统计
		if err := model.DB.Model(&model.Defect{}).Where("project_id = ?", item.UID).Count(&defect_total).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}
		if err := model.DB.Model(&model.Defect{}).Where("status = ? AND project_id = ?", 3, item.UID).Count(&defect_done).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}
		manageProject := model.ManageProject{
			UID:  item.UID,
			Name: item.Name,
			Head: item.Head,
			Task: model.TotalInterface{
				Total: task_total,
				Done:  task_done,
			},
			Demand: model.TotalInterface{
				Total: demand_total,
				Done:  demand_done,
			},
			Defect: model.TotalInterface{
				Total: defect_total,
				Done:  defect_done,
			},
			Overdue: overdue_total,
		}
		manageProjects = append(manageProjects, manageProject)
	}

	return serializer.BuildListResponse(serializer.BuildProjectsManage(manageProjects), uint(total))
}
